package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.ShoppingCardMapper;
import com.sky.service.UserShoppingCardService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class UsershoppingCardServiceImpl implements UserShoppingCardService {
    @Autowired
    private ShoppingCardMapper shoppingCardMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealServiceImpl setmealServiceImpl;

    @Override
    public List list() {
        Long userId = BaseContext.getCurrentId();
        List<ShoppingCart> list = shoppingCardMapper.getList(userId);
        return list;
    }

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {

        /*
         * 如果添加的是个套餐,则不能选择口味
         * */
        if (shoppingCartDTO.getDishId() == null && shoppingCartDTO.getSetmealId() != null) {
            SetmealVO setmeal = setmealServiceImpl.getById(String.valueOf(shoppingCartDTO.getSetmealId()));

            ShoppingCart shoppingCart = new ShoppingCart();

            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setName(setmeal.getName());
            shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setImage(setmeal.getImage());
            shoppingCart.setAmount(setmeal.getPrice());

            //查询这个用户这个套餐之前在数据库里面有没有
            //如果没有
            ShoppingCart temp = new ShoppingCart();
            temp.setUserId(BaseContext.getCurrentId());
            temp.setSetmealId(shoppingCartDTO.getSetmealId());
            log.info("在购物车数据库中查找套餐：" + temp);

            ShoppingCart cardByUserAndSetMeal = (ShoppingCart) getCardByUserAndSetMealIdOrDishId(temp);
            if (cardByUserAndSetMeal == null) {
                shoppingCart.setNumber(1);
                shoppingCardMapper.insert(shoppingCart);
            } else {
                Integer oldNumber = cardByUserAndSetMeal.getNumber();
                Integer newNumber = oldNumber + 1;
                shoppingCart.setNumber(newNumber);
                shoppingCardMapper.delete(cardByUserAndSetMeal);
                shoppingCardMapper.insert(shoppingCart);
            }
            return;
        }

        /*
         * 如果选择的不是套餐而是菜品
         * */
        Long dishId = shoppingCartDTO.getDishId();
        String dishFlavor = shoppingCartDTO.getDishFlavor();

        DishVO dish = dishMapper.getById(Math.toIntExact(dishId));

        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCart.setName(dish.getName());
        shoppingCart.setDishId(dishId);
        shoppingCart.setDishFlavor(dishFlavor);
        shoppingCart.setCreateTime(LocalDateTime.now());
        shoppingCart.setImage(dish.getImage());
        shoppingCart.setAmount(dish.getPrice());

        //如果在数据库中查不到这个用户的这个菜品
        ShoppingCart temp = new ShoppingCart();
        temp.setUserId(BaseContext.getCurrentId());
        temp.setDishId(dishId);
        temp.setDishFlavor(dishFlavor);
        log.info("在购物车数据库中查找菜品：" + temp);
        ShoppingCart cardByUserAndDish = (ShoppingCart) getCardByUserAndSetMealIdOrDishId(temp);

        if (cardByUserAndDish == null) {
            //新增菜品
            shoppingCart.setNumber(1);
            shoppingCardMapper.insert(shoppingCart);
        } else {
            //在原有基础上+1
            Integer oldNumber = cardByUserAndDish.getNumber();
            Integer newNumber = oldNumber + 1;
            shoppingCart.setNumber(newNumber);
            shoppingCardMapper.delete(cardByUserAndDish);
            shoppingCardMapper.insert(shoppingCart);
        }


    }

    @Override
    public void deleteOne(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //如果要删除掉的是一个套餐
        if(shoppingCartDTO.getSetmealId() != null && shoppingCartDTO.getDishId()==null) {
            shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
            //如果套餐只有一份
            ShoppingCart cardByUserAndSetMealIdOrDishId = (ShoppingCart) getCardByUserAndSetMealIdOrDishId(shoppingCart);
            if(cardByUserAndSetMealIdOrDishId.getNumber()<=1) {
                shoppingCardMapper.delete(shoppingCart);
            }
            //套餐有多份
            else {
                BeanUtils.copyProperties(cardByUserAndSetMealIdOrDishId,shoppingCart);
                shoppingCart.setNumber(cardByUserAndSetMealIdOrDishId.getNumber()-1);
                shoppingCardMapper.delete(cardByUserAndSetMealIdOrDishId);
                shoppingCardMapper.insert(shoppingCart);
            }
            return;
        }

        //如果删除的是一个菜品
        shoppingCart.setDishId(shoppingCartDTO.getDishId());
        shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
        //如果这个口味这个菜品只有一个
        ShoppingCart cardByUserAndSetMealIdOrDishId = (ShoppingCart) getCardByUserAndSetMealIdOrDishId(shoppingCart);
        if(cardByUserAndSetMealIdOrDishId.getNumber()<=1){
            shoppingCardMapper.delete(shoppingCart);
        }
        //多个
        else {
            BeanUtils.copyProperties(cardByUserAndSetMealIdOrDishId,shoppingCart);
            shoppingCart.setNumber(cardByUserAndSetMealIdOrDishId.getNumber()-1);
            shoppingCardMapper.delete(cardByUserAndSetMealIdOrDishId);
            shoppingCardMapper.insert(shoppingCart);
        }
    }

    @Override
    public void clean() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCardMapper.delete(shoppingCart);
    }


    private Object getCardByUserAndSetMealIdOrDishId(ShoppingCart temp) {
        return shoppingCardMapper.getByShoppingCart(temp);
    }


}
